package views;

import utils.file.FileSystemModelUtils;
import utils.text.*;
import utils.workspace.*;
import java.io.*;
import java.util.Scanner;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.imageio.ImageIO;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.StyleContext;
import utils.code.*;
import utils.bars.*;
import utils.tabbedpane.*;


/**
 * 
 * @author Juan Carlos García
 */
public class FirstFrame extends JFrame {
 
  private JTree fileTree;
  private FileSystemModelUtils fileSystemModel;
  private static JTextPane fileDetailsTextArea = new JTextPane();
  private static String word = "";
 
  /**
   * Constructor of the FirstFrame.
   * 
   * @param directory Workspace where the system will work
   */
  public FirstFrame(String directory) {
      
    //Initialize Components  
    super("yourJava - Your Own Java Language - v 0.0.1");
    fileDetailsTextArea.setEditable(true);
    fileSystemModel = new FileSystemModelUtils(new File(directory));
    fileTree = new JTree(fileSystemModel);
    fileTree.setEditable(true);
 
    //JTabbedPane
    final JTabbedPane tabbedPane = TabbedPaneUtils.getTabbedPane();
    TabbedPaneUtils.addTab(tabbedPane,"Start Page");
    //MenuBar
    JMenuBar menuBar = MenuBarUtils.getMenuBar(FirstFrame.this,tabbedPane,directory,fileTree);
    //ToolBar
    JToolBar toolBar = ToolBarUtils.getToolBar(tabbedPane,directory,fileTree);
    
    JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,true, new JScrollPane(
        fileTree), new JScrollPane(tabbedPane));
    

    //Adding components
    getContentPane().add(splitPane);
    getContentPane().add(toolBar,BorderLayout.PAGE_START);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setSize(Toolkit.getDefaultToolkit().getScreenSize()); 
    setJMenuBar(menuBar);
    
    
    setVisible(true);
    
    ////////////////////////////////// LISTENERS ////////////////////////////
    
    /**
     * JTreeListener
     */
    fileTree.addMouseListener(new MouseAdapter() {
      public void mousePressed(MouseEvent event) {
        
        if(event.getClickCount() == 2){
            
            File file = (File) fileTree.getLastSelectedPathComponent();

            TabbedPaneUtils.addTab(tabbedPane,file.getName());
            
            JTextPane textArea = (JTextPane) tabbedPane.getComponentAt(tabbedPane.getSelectedIndex());
            
            String code = CodeUtils.replaceCodeOnOpen(file);
            
            textArea.setText(code);

        }
        
      }
    });    
  }
}
 